package org.myorg.quickstart;

public class LDFrame {

    //private int nodeID;

    private int frameNum;

    private byte[] frameBody;

    public LDFrame(/*int nodeID,*/ int frameNum, byte[] frameBody) {
        //this.nodeID = nodeID;
        this.frameNum = frameNum;
        this.frameBody = new byte[frameBody.length];
        System.arraycopy(frameBody, 0, this.frameBody, 0, frameBody.length );
    }

    public LDFrame(byte[] frameData) {
       // this.nodeID = nodeID;
        //this.frameNum = decompressInteger(frameData, 2);
        this.frameNum = (frameData[0]&0xff) | (frameData[1]&0xff)<<8;

        this.frameBody = new byte[frameData.length - 2];

        System.arraycopy(frameData, 2, this.frameBody, 0, frameData.length -2);

    }

    public int getFrameNum() {
        return frameNum;
    }

    public void setFrameNum(int frameNum) {
        this.frameNum = frameNum;
    }

    public byte[] getFrameBody() {
        return frameBody;
    }

    public void setFrameBody(byte[] frameBody) {
        System.arraycopy(frameBody, 0, this.frameBody, 0, frameBody.length );
    }

    public byte[] getBytes() {

        byte[] value = new byte[frameBody.length+2];

        value[0] = (byte)(this.frameNum & 0xFF);
        value[1] = (byte)(this.frameNum  >> 8 & 0xFF);
        System.arraycopy(frameBody, 0, value, 2, frameBody.length);
        return value;
    }

    /*public static byte[] intToByteArray(int i) {
        byte[] result = new byte[4];
        result[0] = (byte)((i >> 24) & 0xFF);
        result[1] = (byte)((i >> 16) & 0xFF);
        result[2] = (byte)((i >> 8) & 0xFF);
        result[3] = (byte)(i & 0xFF);
        return result;
    }

    private static int decompressInteger(byte[] bytes, int byteLength) {
        int value = 0;
        if(byteLength < 0){
            byteLength = bytes.length;
        }
        for (int i = byteLength - 1; i >= 0; i--) {
            for (int bit = 0; bit <= 7; bit++) {
                boolean isSet = ((bytes[i] >>> bit) & 1) == 1;
                if (isSet) {
                    int shift = 8 * (byteLength - 1 - i) + bit;
                    int mask = 1 << shift;
                    value |= mask;
                }
            }
        }
        return value;
    }*/

}
